package com.winse.java.options.style;

/**
 * A color representation similar to {@link java.awt.Color}, but more
 * lightweight, since it does not rquire GUI libraries.
 * 
 * @author Markus Gebhard
 */
public class RGB {
	public static final RGB MAGENTA = new RGB(255, 0, 255);
	public static final RGB GREEN = new RGB(0, 255, 0);
	public static final RGB BLACK = new RGB(0, 0, 0);
	public static final RGB RED = new RGB(255, 0, 0);
	public static final RGB WHITE = new RGB(255, 255, 255);
	public static final RGB ORANGE = new RGB(255, 200, 0);
	public final static RGB CYAN = new RGB(0, 255, 255);
	public final static RGB BLUE = new RGB(0, 0, 255);
	public final static RGB LIGHT_GRAY = new RGB(192, 192, 192);
	public final static RGB GRAY = new RGB(128, 128, 128);
	public final static RGB DARK_GRAY = new RGB(64, 64, 64);
	public final static RGB YELLOW = new RGB(255, 255, 0);
	public final static RGB PINK = new RGB(255, 175, 175);

	private int red;
	private int green;
	private int blue;

	public RGB(int red, int green, int blue) {
		assertColorValueRange(red, green, blue);
		this.red = red;
		this.green = green;
		this.blue = blue;
	}

	private static void assertColorValueRange(int red, int green, int blue) {
		boolean rangeError = false;
		String badComponentString = ""; //$NON-NLS-1$
		if (red < 0 || red > 255) {
			rangeError = true;
			badComponentString = badComponentString + " Red"; //$NON-NLS-1$
		}
		if (green < 0 || green > 255) {
			rangeError = true;
			badComponentString = badComponentString + " Green"; //$NON-NLS-1$
		}
		if (blue < 0 || blue > 255) {
			rangeError = true;
			badComponentString = badComponentString + " Blue"; //$NON-NLS-1$
		}
		if (rangeError == true) {
			throw new IllegalArgumentException("Color parameter outside of expected range:" //$NON-NLS-1$
					+ badComponentString);
		}
	}

	public int getRed() {
		return red;
	}

	public int getGreen() {
		return green;
	}

	public int getBlue() {
		return blue;
	}

	public boolean equals(Object obj) {
		if (!(obj instanceof RGB)) {
			return false;
		}
		RGB other = (RGB) obj;
		return other.getRed() == getRed() && other.getGreen() == getGreen() && other.getBlue() == getBlue();
	}

	public int hashCode() {
		return (red & 0xFF) << 16 | (green & 0xFF) << 8 | blue & 0xFF;
	}

	/**
	 * Returns a string containing a concise, human-readable description of the
	 * receiver.
	 * 
	 * @return a string representation of the <code>RGB</code>
	 */
	public String toString() {
		return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}
	
	  /**
	   * Converts the given Color object to a String contaning the html 
	   * description of the color. E.g.: #FF8080.
	   */
	  public final static String toHex(RGB color) {
	    String red = Integer.toHexString(color.getRed());
	    String green = Integer.toHexString(color.getGreen());
	    String blue = Integer.toHexString(color.getBlue());

	    if (red.length() == 1) {
	      red = "0" + red; //$NON-NLS-1$
	    }
	    if (green.length() == 1) {
	      green = "0" + green; //$NON-NLS-1$
	    }
	    if (blue.length() == 1) {
	      blue = "0" + blue; //$NON-NLS-1$
	    }

	    return "#" + red + green + blue; //$NON-NLS-1$
	  }
}